/**
 * 
 * @authors Tao (972990430@qq.com)
 * @date    2017-07-09 17:37:46
 * @version $Id$
 */
/*-------------------------------------------用户管理--------------------------------*/
backHomeApp.controller('userManageCtrl', ['$scope', "userManageService","DPUtil", "$uibModal", "UIUtil", "$rootScope","storeManageService","itemService",
    function($scope, userManageService,DPUtil, $uibModal, UIUtil,$rootScope,storeManageService,itemService) {
        $scope.conditions = {};
        DPUtil.initPage($scope, $scope.conditions);

        /**
         * [query 查询]
         */
        $scope.query = function(type) {
            if (type) {
                $scope.conditions.page = 1;
            };
            userManageService.list($scope.conditions).then(function(data) {
                $scope.data = data.content;
                $scope.total = data.recordCount;
            });
        };
        $scope.query();

        /**
         * [clean 清除查询条件]
         */
        $scope.clearQuery = function() {
            DPUtil.cleanQuery($scope.conditions);
            $scope.query(1);
        };

        //即时加载角色列表
        (function() {
            userManageService.roleAll().then(function(data) {
                $scope.role_list = data;
            });
            userManageService.adminAll().then(function(data) {
                $scope.admins = data;
            });
            itemService.getItem("所属城市").then(function(data){
                $scope.citys = data;
            });
        })();
        //即时加载门店列表
        (function() {
            storeManageService.list({rows:9999,page:1}).then(function(data) {
                $scope.store_list = data.content;
            });
        })();
        /**
         * [changePageSize 分页数量]
         */
        $scope.changePageSize = function() {
            $scope.conditions.rows = $scope.page_size.value;
            $scope.conditions.page = 1;
            $scope.query();
        };

        /**
         * [add 新增]
         */
        $scope.add = function() {
            _fn.showAdd();
        };

        $scope.update = function(data){
            _fn.showUpdate(data);
        }
        $scope.detail = function(data){
            _fn.showDetail(data);
        }
        /**
         * [delete 删除]
         */
        $scope.delete = function(id) {
            UIUtil.comfirm({
                content: "确定删除该用户吗？",
                ok: function() {
                    userManageService.delete(id).then(function(data) {
                        if (data.success) {
                            UIUtil.alert({ content: data.successMessage });
                            $scope.query();
                        } else {
                            UIUtil.alert({ content: data.failureMessage });
                        };
                    })
                }
            })

        }
        $scope.passwordUpdate = function(id){
            _fn.showPassword(id);
        }
        /**
         * [_fn 私有方法对象]
         * @type {Object}
         */
        var _fn = {
            showAdd: function() {
                $uibModal.open({
                    animation: true,
                    templateUrl: 'add.html',
                    keyboard: false,
                    backdrop: 'static',
                    size:"lg",
                    controller: ["$scope", "$uibModalInstance",
                        function(add_scope, uibModal) {
                            add_scope.user = {};
                            add_scope.title = "添加";
                            add_scope.admins = $scope.admins;
                            add_scope.citys = $scope.citys;
                            add_scope.ok = function() {
                                var role_id = DPUtil.getChecked(add_scope.role_list)?DPUtil.getChecked(add_scope.role_list)[0].id:"";
                                var store_id = DPUtil.getKeyChecked(add_scope.store_list,"id")?DPUtil.getKeyChecked(add_scope.store_list,"id").join(","):"";
                                add_scope.user.roleId = role_id;
                                add_scope.user.companyIds = store_id;
                                userManageService.add(add_scope.user).then(function(data) {
                                    if (data.success) {
                                        UIUtil.alert({ content: data.successMessage });
                                        uibModal.close();
                                        $scope.query();
                                        userManageService.adminAll().then(function(data) {
                                            $scope.admins = data;
                                        });
                                    } else {
                                        UIUtil.alert({ content: data.failureMessage });
                                    }
                                });
                            };

                            add_scope.cancel = function() {
                                uibModal.close();
                            };
                            add_scope.store_list = $scope.store_list;
                            add_scope.role_list = $scope.role_list;
                            add_scope.not_role = true;
                            add_scope.not_store = true;
                            add_scope.role_list = DPUtil.removeAllChecked(add_scope.role_list);
                            add_scope.store_list = DPUtil.removeAllChecked(add_scope.store_list);
                            /**
                             * [toggle 选择角色]
                             */
                            add_scope.toggleStore = function(store) {
                                store.checked = !store.checked;
                                add_scope.not_store = !DPUtil.hasChecked(add_scope.store_list);
                                console.log(add_scope.not_store);
                            };
                            /**
                             * [toggle 选择角色]
                             */
                            add_scope.toggleRole = function(role) {
                                if (!role.checked) {
                                    add_scope.role_list = DPUtil.removeAllChecked(add_scope.role_list);
                                }
                                role.checked = !role.checked;
                                add_scope.not_role = !DPUtil.hasChecked(add_scope.role_list);
                            };
                        }
                    ]
                });
            },
            showUpdate: function(data) {
                $uibModal.open({
                    animation: true,
                    templateUrl: 'update.html',
                    keyboard: false,
                    backdrop: 'static',
                    size:"lg",
                    controller: ["$scope", "$uibModalInstance",
                        function(update_scope, uibModal) {
                            update_scope.citys = $scope.citys;
                            update_scope.user = data;
                            if (update_scope.user.cityId) {
                                update_scope.user.cityId = ""+update_scope.user.cityId;
                            }
                            
                            update_scope.title = "修改";
                            update_scope.admins = $scope.admins;
                            update_scope.ok = function() {
                                var role_id = DPUtil.getChecked(update_scope.role_list)?DPUtil.getChecked(update_scope.role_list)[0].id:"";
                                var store_id = DPUtil.getKeyChecked(update_scope.store_list,"id")?DPUtil.getKeyChecked(update_scope.store_list,"id").join(","):"";
                                update_scope.user.roleId = role_id;
                                update_scope.user.companyIds = store_id;
                                userManageService.update(update_scope.user).then(function(data) {
                                    if (data.success) {
                                        UIUtil.alert({ content: data.successMessage });
                                        uibModal.close();
                                        $scope.query();
                                        userManageService.adminAll().then(function(data) {
                                            $scope.admins = data;
                                        });
                                    } else {
                                        UIUtil.alert({ content: data.failureMessage });
                                    }
                                });
                            };

                            update_scope.cancel = function() {
                                uibModal.close();
                            };
                            update_scope.store_list = $scope.store_list;
                            update_scope.role_list = $scope.role_list;
                            update_scope.not_role = false;
                            update_scope.not_store = false;
                            update_scope.role_list = DPUtil.removeAllChecked(update_scope.role_list);
                            update_scope.store_list = DPUtil.removeAllChecked(update_scope.store_list);
                            // 选择对应角色，店面
                            for (var i = 0; i < update_scope.role_list.length; i++) {
                                if (update_scope.role_list[i].id==data.roleId) {
                                    update_scope.role_list[i].checked = true;
                                }
                            }
                            var store_array = data.companyIds.split(",");
                            for (var i = 0; i < update_scope.store_list.length; i++) {
                                for (var j= 0; j< store_array.length;j++) {
                                    if (update_scope.store_list[i].id==store_array[j]) {
                                        update_scope.store_list[i].checked = true;
                                    }
                                }
                            }
                            /**
                             * [toggle 选择角色]
                             */
                            update_scope.toggleStore = function(store) {
                                store.checked = !store.checked;
                                update_scope.not_store = !DPUtil.hasChecked(update_scope.store_list);
                            };
                            /**
                             * [toggle 选择角色]
                             */
                            update_scope.toggleRole = function(role) {
                                if (!role.checked) {
                                    update_scope.role_list = DPUtil.removeAllChecked(update_scope.role_list);
                                }
                                role.checked = !role.checked;
                                update_scope.not_role = !DPUtil.hasChecked(update_scope.role_list);
                            };
                        }
                    ]
                });
            },
            showDetail: function(data) {
                $uibModal.open({
                    animation: true,
                    templateUrl: 'detail.html',
                    keyboard: false,
                    backdrop: 'static',
                    size:"md",
                    controller: ["$scope", "$uibModalInstance",
                        function(detail_scope, uibModal) {
                            detail_scope.user = data;

                            detail_scope.cancel = function() {
                                uibModal.close();
                            };
                        }
                    ]
                });
            },
            showPassword:function(id){
                $uibModal.open({
                    animation: true,
                    templateUrl: 'backHomeTpl/businessTpl/changePassword.html',
                    keyboard: false,
                    backdrop: 'static',
                    size: "pw",
                    controller: ["$scope", "$uibModalInstance",
                        function(scope, uibModal) {
                            scope.cancel = function() {
                                uibModal.close();
                            };
                            scope.ok = function() {
                                userManageService.updatePassword(id,scope.new_password).then(function(data){
                                    if (data.success) {
                                        UIUtil.alert({ content: "修改成功！" });
                                    }
                                })
                            };
                        } //end fn
                    ]
                }); //end open
            }
        }; //end _fn
    }
]);

/*-------------------------------------------大小项管理--------------------------------*/
backHomeApp.controller('itemManageCtrl', ['$scope', "DPUtil", "itemService", "$uibModal", "UIUtil",
    function($scope, DPUtil, itemService, $uibModal, UIUtil) {
        $scope.conditions = {};
        DPUtil.initPage($scope, $scope.conditions);
        $scope.conditions.type = "";
        /**
         * [query 查询]
         */
        $scope.query = function(type) {
            if (type) {
                $scope.conditions.page = 1;
            };
            itemService.list($scope.conditions).then(function(data) {
                $scope.data = data.content;
                $scope.total = data.recordCount;
            });
        };
        $scope.query();
        /**
         * [changePageSize 分页数量]
         */
        $scope.changePageSize = function() {
            $scope.conditions.rows = $scope.page_size.value;
            $scope.conditions.page = 1;
            $scope.query();
        };

        $scope.clearQuery = function() {
            DPUtil.cleanQuery($scope.conditions);
        }

        $scope.add = function() {
            _fn.showAdd();
        }
        $scope.update = function(key) {
            _fn.showUpdate(key);
        }
        $scope.delete = function(id) {
            UIUtil.comfirm({
                content: "确定删除该大小项吗？",
                ok: function() {
                    itemService.delete(id).then(function(data) {
                        if (data.success) {
                            UIUtil.alert({ content: data.successMessage });
                            $scope.query();
                        } else {
                            UIUtil.alert({ content: data.failureMessage });
                        };
                    })
                }
            })

        }

        /**
         * [_fn 私有方法对象]
         * @type {Object}
         */
        var _fn = {
            /**
             * [showAdd 显示窗口]
             */
            showAdd: function() {
                $uibModal.open({
                    animation: true,
                    templateUrl: 'add.html',
                    keyboard: false,
                    backdrop: 'static',
                    size: "md",
                    controller: ["$scope", "$uibModalInstance",
                        function(add_scope, uibModal) {
                            add_scope.title = "添加";
                            add_scope.item = {};
                            add_scope.item.type = "期数";
                            add_scope.cancel = function() {
                                uibModal.close();
                            }
                            add_scope.ok = function() {
                                itemService.add(add_scope.item).then(function(data) {
                                    if (data.success) {
                                        UIUtil.alert({ content: data.successMessage });
                                        uibModal.close();
                                        $scope.query();
                                    } else {
                                        UIUtil.alert({ content: data.failureMessage });
                                    }
                                });
                            }
                        }
                    ]
                });
            },
            showUpdate: function(data) {
                $uibModal.open({
                    animation: true,
                    templateUrl: 'add.html',
                    keyboard: false,
                    backdrop: 'static',
                    size: "md",
                    controller: ["$scope", "$uibModalInstance",
                        function(update_scope, uibModal) {
                            update_scope.title = "修改";
                            update_scope.item =data;
                            update_scope.cancel = function() {
                                uibModal.close();
                            }
                            update_scope.ok = function() {
                                itemService.update(update_scope.item).then(function(data) {
                                    if (data.success) {
                                        UIUtil.alert({ content: data.successMessage });
                                        uibModal.close();
                                        $scope.query();
                                    } else {
                                        UIUtil.alert({ content: data.failureMessage });
                                    }
                                });
                            }
                        }
                    ]
                });
            }
        };
    }
]);


/*-------------------------------------------权限管理--------------------------------*/
backHomeApp.controller('roleManageCtrl', ['$scope', "DPUtil", "$uibModal", "UIUtil","$rootScope",
    function($scope, DPUtil, $uibModal, UIUtil,$rootScope) {
        $scope.conditions = {};
        DPUtil.initPage($scope, $scope.conditions);

        /**
         * [query 查询]
         */
        $scope.query = function(type) {
            if (type) {
                $scope.conditions.page = 1;
            };
            roleService.list($scope.conditions).then(function(data) {
                $scope.data = data.rows;
                $scope.total = data.total;
            });
        };
        // $scope.query();


        //即时加载角色列表
        // (function() {
        //     privilegeService.list().then(function(data) {
        //         $scope.authority_list = data.data;
        //     });
        // })();
        $scope.authority_list = $rootScope.datas;
        /**
         * [changePageSize 分页数量]
         */
        $scope.changePageSize = function() {
            $scope.conditions.rows = $scope.page_size.value;
            $scope.conditions.page = 1;
            $scope.query();
        };

        /**
         * [add 新增]
         */
        $scope.add = function() {
            _fn.showAdd();
        };

        /**
         * [update]
         */
        $scope.update = function() {
            var role = DPUtil.getSelect($scope.data);
            if (role.length == 0) {
                UIUtil.alert({ content: "请选择一个角色！" });
            } else {
                roleService.find(role.id).then(function(data) {
                    if (data.code == 0) {
                        _fn.showUpdate(data.data);
                    };
                });
            }
        };

        /**
         * [del 删除]
         */
        $scope.del = function() {
            var role = DPUtil.getSelect($scope.data);
            if (role.length == 0) {
                UIUtil.alert({ content: "请选择一个角色！" });
            } else {
                UIUtil.comfirm({
                    content: "是否删除选定的角色，删除后会导致人员相对应的权限也同时被删除？",
                    ok: function() {
                        roleService.del(role.id).then(function() {
                            $scope.query();
                            UIUtil.alert({ content: "删除成功！" });
                        });
                    }
                });
            }
        };

        /**
         * [_fn 私有方法对象]
         * @type {Object}
         */
        var _fn = {
            /**
             * [showAdd 显示窗口]
             */
            showAdd: function() {
                $uibModal.open({
                    animation: true,
                    templateUrl: 'add.html',
                    keyboard: false,
                    backdrop: 'static',
                    size: "lg",
                    controller: ["$scope", "$uibModalInstance",
                        function(add_scope, uibModal) {
                            add_scope.role = {};
                            add_scope.ok = function() {
                                var role = {
                                    rolename: add_scope.role.name,
                                    remark: add_scope.role.remark
                                };
                                var first_menc = DPUtil.getChecked(add_scope.authority_list);
                                var second_menc = [];
                                for (var i = 0; i < add_scope.authority_list.length; i++) {
                                    for (var j = 0; j < add_scope.authority_list[i].children.length; j++) {
                                        if (add_scope.authority_list[i].children[j].checked) {
                                            second_menc.push(add_scope.authority_list[i].children[j]);
                                        }
                                    }
                                }
                                role.rpList = first_menc.concat(second_menc);

                                roleService.add(role).then(function(data) {
                                    $scope.query();
                                    uibModal.close();
                                    UIUtil.alert({ content: "新增成功" });
                                });
                            };
                            add_scope.cancel = function() {
                                uibModal.close();
                                _fn.cleanAuthority();
                            };

                            //权限数据
                            add_scope.authority_list = $scope.authority_list;
                            add_scope.not_authority = true;
                            add_scope.status = {
                                info_open: true,
                                authority_open: false
                            };

                            /**
                             * [toggle 选择权限]
                             */
                            add_scope.toggle = function(authority) {
                                authority.checked = !authority.checked;
                                for (var i = 0; i < authority.children.length; i++) {
                                    if (authority.checked) {
                                        authority.children[i].checked = true;
                                    } else {
                                        authority.children[i].checked = false;
                                    }
                                }
                                add_scope.not_authority = !DPUtil.hasChecked(add_scope.authority_list);
                            };

                            /**
                             * [toggleChild 选择子菜单]
                             * @param  {[type]} child     [子对象]
                             * @param  {[type]} authority [父对象]
                             */
                            add_scope.toggleChild = function(child, authority) {
                                child.checked = !child.checked;
                                var is_all = false;
                                for (var i = 0; i < authority.children.length; i++) {
                                    if (authority.children[i].checked) {
                                        is_all = true;
                                    }
                                }
                                authority.checked = is_all;
                                add_scope.not_authority = !DPUtil.hasChecked(add_scope.authority_list);
                            };
                        }
                    ]
                });
            },
            showUpdate: function(data) {
                $uibModal.open({
                    animation: true,
                    templateUrl: 'update.html',
                    keyboard: false,
                    backdrop: 'static',
                    size: "lg",
                    controller: ["$scope", "$uibModalInstance",
                        function(update_scope, uibModal) {
                            update_scope.role = {
                                name: data.rolename,
                                remark: data.remark
                            };
                            update_scope.ok = function() {
                                var role = {
                                    id: data.id,
                                    rolename: update_scope.role.name,
                                    remark: update_scope.role.remark
                                };
                                var first_menc = DPUtil.getChecked(update_scope.authority_list);
                                var second_menc = [];
                                for (var i = 0; i < update_scope.authority_list.length; i++) {
                                    for (var j = 0; j < update_scope.authority_list[i].children.length; j++) {
                                        if (update_scope.authority_list[i].children[j].checked) {
                                            second_menc.push(update_scope.authority_list[i].children[j]);
                                        }
                                    }
                                }
                                role.rpList = first_menc.concat(second_menc);
                                roleService.update(role).then(function() {
                                    $scope.query();
                                    uibModal.close();
                                    UIUtil.alert({ content: "修改成功" });
                                });
                            };
                            update_scope.cancel = function() {
                                uibModal.close();
                                _fn.cleanAuthority();
                            };

                            //权限数据
                            update_scope.authority_list = $scope.authority_list;
                            update_scope.status = {
                                info_open: true,
                                authority_open: false
                            };
                            if (data.rpList) {
                                for (var k = 0; k < data.rpList.length; k++) {
                                    for (var i = 0; i < update_scope.authority_list.length; i++) {
                                        if (update_scope.authority_list[i].id == data.rpList[k].privilegeid) {
                                            update_scope.authority_list[i].checked = true;
                                            break;
                                        }
                                        for (var j = 0; j < update_scope.authority_list[i].children.length; j++) {
                                            if (update_scope.authority_list[i].children[j].id == data.rpList[k].privilegeid) {
                                                update_scope.authority_list[i].children[j].checked = true;
                                                update_scope.authority_list[i].checked = true;
                                                break;
                                            }
                                        }
                                    }
                                }
                            }

                            update_scope.not_authority = false;

                            /**
                             * [toggle 选择权限]
                             */
                            update_scope.toggle = function(authority) {
                                authority.checked = !authority.checked;
                                for (var i = 0; i < authority.children.length; i++) {
                                    if (authority.checked) {
                                        authority.children[i].checked = true;
                                    } else {
                                        authority.children[i].checked = false;
                                    }
                                }
                                update_scope.not_authority = !DPUtil.hasChecked(update_scope.authority_list);
                            };

                            /**
                             * [toggleChild 选择子菜单]
                             * @param  {[type]} child     [子对象]
                             * @param  {[type]} authority [父对象]
                             */
                            update_scope.toggleChild = function(child, authority) {
                                child.checked = !child.checked;
                                var is_all = false;
                                for (var i = 0; i < authority.children.length; i++) {
                                    if (authority.children[i].checked) {
                                        is_all = true;
                                    }
                                }
                                authority.checked = is_all;
                                update_scope.not_authority = !DPUtil.hasChecked(update_scope.authority_list);
                            };
                        }
                    ]
                });
            },
            cleanAuthority: function() {
                for (var i = 0; i < $scope.authority_list.length; i++) {
                    $scope.authority_list[i].checked = false;
                    for (var j = 0; j < $scope.authority_list[i].children.length; j++) {
                        $scope.authority_list[i].children[j].checked = false;
                    }
                }
            }
        }; //end _fn
    }
]);

